package net.siufung.market.base.permit.service.impl;

import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeNodeConfig;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.RandomUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.google.common.collect.Lists;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import net.siufung.core.model.enums.DeletedEnum;
import net.siufung.market.base.permit.controller.model.vo.PermitTreeNode;
import net.siufung.market.base.permit.mapper.IMenuMapper;
import net.siufung.market.base.permit.mapper.IPermitMapper;
import net.siufung.market.base.permit.mapper.entity.Menu;
import net.siufung.market.base.permit.mapper.entity.Permit;
import net.siufung.market.base.permit.service.IPermitQueryService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 陈建峰
 * @since 2022/7/20 9:38 下午
 */
@Slf4j
@AllArgsConstructor
@Service(value = "permitQueryService")
@Transactional(readOnly = true, rollbackFor = Exception.class)
public class PermitQueryServiceImpl implements IPermitQueryService {

    private final IPermitMapper permitMapper;
    private final IMenuMapper menuMapper;

    @Override
    public List<Tree<Integer>> getTreeList() {
        PermitTreeNode permitTreeNode;
        List<PermitTreeNode> trees = Lists.newArrayList();
        QueryWrapper<Permit> permitWrapper = new QueryWrapper<>();
        permitWrapper.lambda().eq(Permit::getDeleted, DeletedEnum.NOT.getValue());
        permitWrapper.lambda().isNotNull(Permit::getParentId);
        List<Permit> permitList = permitMapper.selectList(permitWrapper);
        for (Permit permit: permitList) {
            permitTreeNode = new PermitTreeNode();
            permitTreeNode.setParentId(permit.getMenuId());
            permitTreeNode.setId(RandomUtil.randomInt());
            permitTreeNode.setCode(StrUtil.toString(permit.getId()));
            permitTreeNode.setName(permit.getText());
            trees.add(permitTreeNode);
        }
        QueryWrapper<Menu> menuWrapper = new QueryWrapper<>();
        menuWrapper.lambda().eq(Menu::getDeleted, DeletedEnum.NOT.getValue());
        menuWrapper.lambda().orderByAsc(Menu::getSequence);
        List<Menu> menuList = menuMapper.selectList(menuWrapper);
        for (Menu menu: menuList) {
            permitTreeNode = new PermitTreeNode();
            permitTreeNode.setId(menu.getId());
            permitTreeNode.setParentId(menu.getParentId());
            permitTreeNode.setCode(StrUtil.concat(false, "menu-", StrUtil.toString(menu.getId())));
            permitTreeNode.setName(menu.getMenuName());
            trees.add(permitTreeNode);
        }
        TreeNodeConfig treeNodeConfig = new TreeNodeConfig();
        return TreeUtil.build(trees, 1, treeNodeConfig, (treeNode, tree) -> {
            tree.setId(treeNode.getId());
            tree.setParentId(treeNode.getParentId());
            tree.setWeight(treeNode.getWeight());
            tree.setName(treeNode.getName());
            tree.putExtra("code", treeNode.getCode());
        });
    }
}
